package com.Server;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;









import com.Server.DataBase.BanIpTable;
import com.Server.DataBase.DatabaseFactoryLogin;
import com.Server.DataBase.OkIpTable;
import com.Server.DataBase.UuidTable;
import com.Server.model.TEST;



public class Login {
	private static final Logger _log = Logger.getLogger(Login.class.getName());
	private static final String LOG_PROP = "./config/log.properties";
	 public static String SERVER_IP;
	 public static int SERVER_PORT;
	 private static final ConcurrentHashMap<String , String> UUID_LIST = new ConcurrentHashMap<String , String>(128);
	 
	 public static String getUUidList(String ip){
		 String uuid = UUID_LIST.get(ip);
		 if(uuid == null){
			 uuid = "錯誤";
		 }
		 return uuid;
	 }
	 
	 public static void putUUidList(String ip , String uuid){
		 UUID_LIST.put(ip, uuid);
	 }
	    
	public static void main(String[] args) throws IOException, SQLException {
		final File logFolder = new File("log");
		logFolder.mkdir();
		try {
			final InputStream is = new BufferedInputStream(new FileInputStream(LOG_PROP));
			LogManager.getLogManager().readConfiguration(is);
			is.close();
		} catch (final IOException e) {
			_log.log(Level.SEVERE, "Failed to Load " + LOG_PROP + " File.", e);
			System.exit(0);
		}
//		URL url = new URL("https://dl.dropboxusercontent.com/u/61561180/ServerIp.ini");
//		 InputStream src = null;
//		src = url.openStream();
//		BufferedReader br = new BufferedReader(new InputStreamReader(src)); 
//		SERVER_IP =br.readLine();
//		SERVER_PORT = Integer.valueOf(br.readLine());
		Config.load();
		DatabaseFactoryLogin.setDatabaseSettings();
		DatabaseFactoryLogin.get();
		BanIpTable.getInstance().getIpTable();
		OkIpTable.get().getIpTable();
		UuidTable.getInstance().load();
		System.out.println("監聽伺服器啟用中...!!!");
		// 監聽端口啟動重置作業
		EchoServerTimer.get().start();
		EchoServerTimer2.get().start();
		TEST tt = new TEST();
		GeneralThreadPool.getInstance().execute(tt);
	}
}
